<template>
	<div class="login_container">
		<!-- 注册盒子-->
		<div class="login_box">
			<!-- 头像盒子-->
			<div class="img_box">
				<!--  <img src="./assets/logo.png" /> -->
			</div>
			<!-- 表单-->
			<div class="form_box">
				<el-form ref="form" :model="form" label-width="80px" status-icon :rules='rules' class="demo-ruleForm">
					<el-form-item label="账号" prop="account">
						<el-input v-model="form.account"></el-input>
					</el-form-item>
					<el-form-item label="昵称">
						<el-input v-model="form.nickname"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input type="password" v-model="form.password"></el-input>
					</el-form-item>
					<el-form-item label="确认密码" prop="checkPass">
						<el-input type="password" v-model="form.checkPass"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="reg()">注册</el-button>
						<el-button>重置</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
	</div>
</template>

<script>
	/* 导出组件 其他地方才可以导入组件*/
	export default {
		data() {
			var validatePass3 = (rule, value, callback) => {
							if (value === '') {
								callback(new Error('请输入账号'));
							} else {
								if (this.form.checkPass !== '') {
									this.$refs.form.validateField('checkPass');
								}
								this.$http.get("/user/accountExist/" + value).then(res => {
									if (res.data.code == 200) {	
										callback();
									} else {
										callback(new Error(res.data.msg));
									}
								
								})
							}
						};
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.form.checkPass !== '') {
						this.$refs.form.validateField('checkPass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.form.password) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				form: {
					account: "",
					password: "",
					checkPass: "",
					nickname: ""
				},
				rules: {
					password: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					checkPass: [{
						validator: validatePass2,
						trigger: 'blur'
					}],
					account: [{
						validator: validatePass3,
						trigger: 'blur'
					}],
				},
				flag: true
			}
		},
		methods: {
			reg() {
				var that = this;
				//向后端发送ajax请求,提交表单的数据   json格式提交 
				this.$http.post('/user/reg', this.form).then(function(res) { //回调
					if (res.data.code == 200) {
						//跳转到登录页面
						that.$router.push("/login");
						that.$message({
							message: res.data.msg,
							type: 'success'
						});
					} else if (res.data.code == 201) {
						that.$message({
							message: res.data.msg,
							type: 'warning'
						});
					} else {
						that.$message.error({
							message: res.data.msg
						});
					}
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						alert('submit!');
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		},

	}
</script>

<style scoped="scoped">
	html,
	body,
	.login_container {
		height: 100%;
		margin: 0px;
		padding: 0px;
	}

	.login_container {
		background-color: #00a7fa;
	}

	.login_box {
		width: 450px;
		height: 350px;
		background-color: #fff;
		border-radius: 10px;
		position: absolute;
		left: 50%;
		top: 50%;
		margin-top: -175px;
		margin-left: -225px;
	}

	.img_box {
		width: 130px;
		height: 130px;
		position: absolute;
		left: 50%;
		transform: translate(-50%, -50%);
		background-color: #fff;
		border-radius: 50%;
		padding: 5px;
		border: 1px solid #eee;
	}

	.img_box img {
		width: 100%;
		height: 100%;
		border-radius: 50%;
		background-color: #eee;
	}

	.form_box {
		margin-top: 100px;
		padding: 10px;
	}
</style>
